package com.lecampus.businessSecond_hand_transactions.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lecampus.businessSecond_hand_transactions.entity.Product;
import com.lecampus.businessSecond_hand_transactions.entity.ProductCategory;
import com.lecampus.businessSecond_hand_transactions.entity.ProductImages;
import com.lecampus.businessSecond_hand_transactions.mapper.ProductCategoryMapper;
import com.lecampus.businessSecond_hand_transactions.mapper.ProductImagesMapper;
import com.lecampus.businessSecond_hand_transactions.mapper.ProductMapper;
import com.lecampus.businessSecond_hand_transactions.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lecampus.businessSecond_hand_transactions.vo.ProductImageVO;
import com.lecampus.businessSecond_hand_transactions.vo.ProductInfoVO;
import com.lecampus.businessSecond_hand_transactions.vo.ProductCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-03-24
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductCategoryMapper categoryMapper;
    @Autowired
    private ProductImagesMapper imagesMapper;

    private ProductImageVO productImageVO;
    private ProductInfoVO productInfoVO;
    private ProductCategoryVO productCategoryVO;
    private List<ProductImages> productImagesList;
    private List<ProductCategory> productCategoryList;
    private List<ProductImageVO> productImageVOList;
    private List<ProductInfoVO> productInfoVOList;
    private List<ProductCategoryVO> productCategoryVOList;

    /**
     * 查询所有二手商品信息
     */
    @Override
    public List<ProductCategoryVO> findAll() {

        QueryWrapper wrapper = new QueryWrapper<>();

        //分类
        //找到所有分类
        productCategoryList = categoryMapper.selectList(wrapper);

        //商品
        productCategoryVOList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {
            wrapper = new QueryWrapper<>();
            //根据 category_id 拿到对应的所有商品列表
            wrapper.eq("category_id", productCategory.getCategoryId());

            List<Product> productList = productMapper.selectList(wrapper);

            //图片
            productInfoVOList = new ArrayList<>();
            for (Product product : productList) {
                wrapper = new QueryWrapper<>();
                //先根据 product_id 拿到对应的所有图片列表
                wrapper.eq("product_id", product.getProductId());

                productImagesList = imagesMapper.selectList(wrapper);

                productImageVOList = new ArrayList<>();
                for (ProductImages productImages : productImagesList) {
                    //封装图片
                    productImageVO = new ProductImageVO();
                    BeanUtils.copyProperties(productImages, productImageVO);

                    productImageVOList.add(productImageVO);
                }

                //封装商品
                productInfoVO = new ProductInfoVO();
                BeanUtils.copyProperties(product, productInfoVO);

                //商品中的图片
                productInfoVO.setImageList(productImageVOList);
                productInfoVOList.add(productInfoVO);
            }

            //封装分类
            productCategoryVO = new ProductCategoryVO();
            BeanUtils.copyProperties(productCategory, productCategoryVO);

            //分类中的商品
            productCategoryVO.setProductList(productInfoVOList);
            productCategoryVOList.add(productCategoryVO);
        }
        return productCategoryVOList;
    }

    /**
     * 根据用户id查找二手商品
     */
    @Override
    public List<ProductCategoryVO> findByUserId(String userId) {

        QueryWrapper wrapper = new QueryWrapper<>();

        wrapper.eq("user_id",userId);

        List<Product> productList = productMapper.selectList(wrapper);

        //图片
        productCategoryVOList = new ArrayList<>();
        for (Product product : productList) {

            //根据 product 的 category_id 找到商品对应的分类列表
            wrapper = new QueryWrapper<>();
            wrapper.eq("category_id",product.getCategoryId());

            productCategoryList = categoryMapper.selectList(wrapper);

            for (ProductCategory productCategory : productCategoryList) {

                wrapper = new QueryWrapper<>();
                //先根据 product_id 拿到对应的所有图片列表
                wrapper.eq("product_id", product.getProductId());

                productImagesList = imagesMapper.selectList(wrapper);

                productImageVOList = new ArrayList<>();
                for (ProductImages productImages : productImagesList) {
                    //封装图片
                    productImageVO = new ProductImageVO();
                    BeanUtils.copyProperties(productImages, productImageVO);

                    productImageVOList.add(productImageVO);
                }

                //封装商品
                productInfoVO = new ProductInfoVO();
                BeanUtils.copyProperties(product, productInfoVO);

                //商品中的图片
                productInfoVOList = new ArrayList<>();
                productInfoVO.setImageList(productImageVOList);
                productInfoVOList.add(productInfoVO);

                //封装分类
                productCategoryVO = new ProductCategoryVO();
                BeanUtils.copyProperties(productCategory, productCategoryVO);

                //分类中的商品
                productCategoryVO.setProductList(productInfoVOList);
                productCategoryVOList.add(productCategoryVO);
            }
        }

        return productCategoryVOList;
    }

    /**
     * 增加商品
     */
    @Override
    public void inputProduct(Product product) {
        productMapper.insert(product);
    }

    /**
     * 删除商品
     */
    @Override
    public void deleteProductById(String productId) {
        productMapper.deleteById(productId);
    }
}